package com.atldy.commerce.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * @version 1.0
 * @Author: lidongyang
 * @CreateTime: 2021-08-16 7:19
 * @Description： 商品状态吗
 */
@Getter
@AllArgsConstructor
public enum GoodsStatus {

    ONLINE(101, "上线"),
    OFFLINE(102, "下线"),
    STOCK_OUT(103, "缺货");

    /**
     * 状态码
     */
    @EnumValue
    @JsonValue    //标记响应json值
    private final Integer status;

    /**
     * 状态描述
     */
    private final String description;

    public static GoodsStatus of(Integer status) {
        Objects.requireNonNull(status);
        GoodsStatus statusConstant = Stream.of(values())
                .filter(goodsStatusConstant -> goodsStatusConstant.getStatus().equals(status))
                .findAny()
                .orElseThrow(() -> new IllegalArgumentException(status + " not exists"));
        return statusConstant;
    }

    @Override
    public String toString() {
        return this.description;
    }
}